package com.zhoug.common3.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 用于获取android设备信息的工具
 *
 * @Author: zhoug
 * @Date: 2022/9/19
 * @Description:
 */
public class DeviceUtils {
    private static final String TAG = ">>>DeviceUtils";

    /**
     * 获取设备唯一ID
     *
     * @param context
     * @return IMEI或者MEID
     */
    @SuppressLint("all")
    public static String getDeviceId(@NonNull Context context) {
        String deviceId = null;
        TelephonyManager telephonyManager = getTelephonyManager(context);

        if (telephonyManager != null) {
            deviceId = getDeviceId(telephonyManager);
            if (StringUtils.isNullOrEmpty(deviceId)) {
                deviceId = getImei(telephonyManager);
            }
            if (StringUtils.isNullOrEmpty(deviceId)) {
                deviceId = getSimSerialNumber(telephonyManager);
            }
        }
        if (StringUtils.isNullOrEmpty(deviceId)) {
            deviceId = getSettingsSecure(context, Settings.Secure.ANDROID_ID);
        }
        LogUtils.d(TAG, "getDeviceId:" + deviceId);
        return StringUtils.isNullOrEmpty(deviceId) ? null : deviceId;
    }


    /**
     * 获取手机号
     *
     * @param context
     * @return
     */
    @SuppressLint("all")
    public static String getPhoneNumber(@NonNull Context context) {
        String phoneNumber = null;
        TelephonyManager telephonyManager = getTelephonyManager(context);
        if (telephonyManager != null) {
            phoneNumber = telephonyManager.getLine1Number();
        }
        if (StringUtils.isNullOrEmpty(phoneNumber)) {


        }
        return StringUtils.isNullOrEmpty(phoneNumber) ? null : phoneNumber;
    }

    /**
     * 获取手机服务商信息
     *
     * @param context
     * @return
     */
    @SuppressLint({"MissingPermission", "HardwareIds"})
    public static String getProvidersName(@NonNull Context context) {
        String providersName = null;
        String subscriberId = null;
        TelephonyManager telephonyManager = getTelephonyManager(context);
        if (telephonyManager != null) {
            subscriberId = telephonyManager.getSubscriberId();
        }
        LogUtils.d(TAG, "getProvidersName:subscriberId=" + subscriberId);

        return providersName;

    }


    public static Map<String, String> getPhoneHardwareInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("制造商", Build.MANUFACTURER);
        map.put("品牌", Build.BRAND);
        map.put("设备参数", Build.DEVICE);
        map.put("型号", Build.MODEL);
        map.put("版本号", Build.DISPLAY);
        map.put("CPU架构", Build.CPU_ABI);
        map.put("处理器", Build.HARDWARE);
        return map;
    }


    /**
     * 获取电话管理器
     *
     * @param context Context
     * @return TelephonyManager
     */
    public static @Nullable
    TelephonyManager getTelephonyManager(@NonNull Context context) {
        try {
            return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        } catch (Exception e) {
            LogUtils.e(TAG, "getTelephonyManager:" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 封装Settings.Secure.getString方法
     *
     * @param context
     * @param name    {@link  Settings.Secure} eg:Settings.Secure.ANDROID_ID
     * @return String
     */
    public static String getSettingsSecure(@NonNull Context context, String name) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), name);
        } catch (Exception e) {
            LogUtils.e(TAG, "getSettingsSecure:" + e.getMessage());
        }
        return null;
    }

    @SuppressLint("all")
    private static String getSimSerialNumber(TelephonyManager telephonyManager) {
        if (telephonyManager == null) return null;
        try {
            String simSerialNumber= telephonyManager.getSimSerialNumber();
            LogUtils.d(TAG, "getSimSerialNumber:"+simSerialNumber);
            return simSerialNumber;
        } catch (Exception e) {
            LogUtils.e(TAG, "getSimSerialNumber:" + e.getMessage());
//            e.printStackTrace();
        }
        return null;
    }

    @SuppressLint("all")
    private static String getImei(TelephonyManager telephonyManager) {
        if (telephonyManager == null) return null;
        try {
            String imei= telephonyManager.getImei();
            LogUtils.d(TAG, "getImei:"+imei);
            return imei;
        } catch (Exception e) {
            LogUtils.e(TAG, "getImei:" + e.getMessage());
//            e.printStackTrace();
        }
        return null;
    }

    @SuppressLint("all")
    private static String getDeviceId(TelephonyManager telephonyManager) {
        if (telephonyManager == null) return null;
        try {
            String deviceid = telephonyManager.getDeviceId();
            LogUtils.d(TAG, "getDeviceId:"+deviceid);
            return deviceid;
        } catch (Exception e) {
            LogUtils.e(TAG, "_getDeviceId:" + e.getMessage());
//            e.printStackTrace();
        }
        return null;
    }


}
